/** 定义新闻相关接口 */
const express = require("express");
const router = express.Router();
const Joi = require("joi");
const Response = require("../utils/Response");

// 引入mysql连接池
const pool = require("../utils/pool");

//** 添加博主 */
router.post("/feature-blogger/authtor-add", (req, res) => {
  let { blg_name, motto, source, avatar, fans, attention } = req.body;
  let schema = Joi.object({
    blg_name: Joi.string().required(),
    motto: Joi.string().required(),
    source: Joi.string().required(),
    avatar: Joi.string().required(),
    fans: Joi.number().required(),
    attention: Joi.number().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    console.log(error);
    res.send(Response.error(400, error));
    return;
  }
  let obj = { blg_name, motto, source, avatar, fans, attention };
  const sql = "insert into blogger_info set ?";
  pool.query(sql, obj, (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
  });
});

/** 查询所有博主 */
router.get("/feature-blogger/author", (req, res) => {
  let sql = "select * from blogger_info";
  pool.query(sql, (error, result) => {
    res.send(Response.ok(result));
  });
});

/** 模糊查询博主 */
router.post("/feature-blogger/author-name", (req, res) => {
  let { name } = req.body;
  let schema = Joi.object({
    name: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  let sql = "select * from blogger_info where blg_name like ?";
  pool.query(sql, [`%${name}%`], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw err;
    }
    res.send(Response.ok(result));
  });
});
/** 全名查询博主 */
router.get('/feature-blogger/author-allname',(req,res)=>{
  let { name } = req.query;
  let schema = Joi.object({
    name: Joi.string().required(),
  })
  let { error, value } = schema.validate(req.query);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  let sql = "select * from blogger_info where blg_name = ?";
  pool.query(sql, [name], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw err;
    }
    res.send(Response.ok(result));
  });
})

/** 删除博主 */
router.post("/feature-blogger/author-delete", (req, res) => {
  let { blogger_id } = req.body;
  let schema = Joi.object({
    blogger_id: Joi.number().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  let sql = "delete from blogger_info where id=?";
  pool.query(sql, [blogger_id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw err;
    }
    res.send(Response.ok(result));
  });
});

/** 修改博主 */
router.post("/feature-blogger/authtor-update", (req, res) => {
  let { blogger_id, blg_name, motto, source, avatar, fans, attention } =
    req.body;
  let schema = Joi.object({
    blogger_id: Joi.number().required(),
    blg_name: Joi.string().required(),
    motto: Joi.string().required(),
    source: Joi.string().required(),
    avatar: Joi.string().required(),
    fans: Joi.number().required(),
    attention: Joi.number().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    console.log(error);
    res.send(Response.error(400, error));
    return;
  }
  const sql =
    "update blogger_info set blg_name=?, motto=?, source=?, avatar=?, fans=?, attention=? where id=?";
  pool.query(
    sql,
    [blg_name, motto, source, avatar, fans, attention, blogger_id],
    (error, result) => {
      if (error) {
        res.send(Response.error(500, error));
        throw error;
      }
      res.send(Response.ok());
    }
  );
});

/******************视频**************** */
/** 新增视频 */
router.post("/feature-blogger/video-add", (req, res) => {
  //   console.log(1);
  let { author, duration, describle, time, video_img, video } = req.body;
  let schema = Joi.object({
    author: Joi.string().required(),
    duration: Joi.string().required(),
    describle: Joi.string().required(),
    time: Joi.string().required(),
    video_img: Joi.string().required(),
    video: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    console.log(error);
    res.send(Response.error(400, error));
    return;
  }
  let obj = {
    author,
    duration,
    describle,
    time,
    video_img,
    video,
  };
  let sql = "insert into video set ?";
  pool.query(sql, [obj], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
  });
});

/** 修改视频信息 */
router.post("/feature-blogger/video-update", (req, res) => {
  let { id, author, duration, describle, time, video_img, video } = req.body;
  let schema = Joi.object({
    id: Joi.number().required(),
    author: Joi.string().required(),
    duration: Joi.string().required(),
    describle: Joi.string().required(),
    time: Joi.string().required(),
    video_img: Joi.string().required(),
    video: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    console.log(error);
    res.send(Response.error(400, error));
    return;
  }
  let sql =
    "update video set author=?, duration=?, describle=?, time=?, video_img=?, video=? where id=?";
  pool.query(
    sql,
    [author, duration, describle, time, video_img, video, id],
    (error, result) => {
      if (error) {
        res.send(Response.error(500, error));
        throw error;
      }
      res.send(Response.ok());
      console.log(result);
    }
  );
});

/** 查询所有视频 */
router.get("/feature-blogger/videos", (req, res) => {
  let sql = "select * from video";
  pool.query(sql, (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok(result));
  });
});

/** 名字查询视频 */
router.get("/feature-blogger/video-author", (req, res) => {
  let { author } = req.query;
  let schema = Joi.object({
    author: Joi.string().required(),
  });
  let { error, value } = schema.validate(req.query);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  let sql = "select * from video where author=?";
  pool.query(sql, [author], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok(result));
  });
});

/** 删除视频信息 */
router.post("/feature-blogger/video-del", (req, res) => {
  let { id } = req.body;
  let schema = Joi.object({
    id: Joi.number().required(),
  });
  let { error, value } = schema.validate(req.body);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  let sql = "delete from video where id=?";
  pool.query(sql, [id], (error, result) => {
    if (error) {
      res.send(Response.error(500, error));
      throw error;
    }
    res.send(Response.ok());
  });
});

/** 分页查询视频 */
router.get('/feature-blogger/videos-page',async (req,res)=>{
  let {page,pagesize} = req.query
  let schema = Joi.object({
    page:Joi.number().required(),
    pagesize: Joi.number().required()
  })
  let { error, value } = schema.validate(req.query);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  try {
    let startIndex = (page - 1) * pagesize;
    let size = parseInt(pagesize);
    let sql = "select * from video limit ?,?";
    let result = await pool.querySync(sql, [ startIndex, size]);
    // 执行查询总条目数
    let sql2 = "select count(*) as count from video";
    let result2 = await pool.querySync(sql2);
    let total = result2[0].count;
    console.log(total);
    let Pages = Math.ceil(total /pagesize)
    res.send(
      Response.ok({ page: parseInt(page),Pages:Pages, pagesize: size, total, result })
    );
  } catch (error) {
    res.send(Response.error(error));
  }
})

/** id查询视频 */
router.get('/feature-blogger/video-id',(req,res)=>{
  let {id}= req.query
  let schema= Joi.object({
    id: Joi.number().required()
  })
  let { error, value } = schema.validate(req.query);
  if (error) {
    res.send(Response.error(400, error));
    return; // 结束
  }
  let sql = 'select * from video where id = ?'
  pool.query(sql,[id],(error,result)=>{
    if (error) {
      res.send(Response.error(500, error));
      throw err;
    }
    res.send(Response.ok(result));
  })
})

// 将router对象导出
module.exports = router;
